package trlygjj.tanruiliyigenjinjin.web.controller;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import trlygjj.tanruiliyigenjinjin.domain.Permission;
import trlygjj.tanruiliyigenjinjin.page.PageResult;
import trlygjj.tanruiliyigenjinjin.query.QueryObject;
import trlygjj.tanruiliyigenjinjin.service.IPermissionService;
import trlygjj.tanruiliyigenjinjin.util.JsonResult;
import trlygjj.tanruiliyigenjinjin.util.PermissionName;

import java.util.List;

/**
 * Created by user on 2018/7/11.
 */
@Controller
@RequestMapping("permission")
public class PermissionController {

    @Autowired
    private IPermissionService permissionService;

    /**
     * 提供一个默认不输入后缀,直接可以跳转到具有分页的列表页面
     * @return
     */
    @RequiresPermissions("permission:index")
    @PermissionName("权限主页")
    @RequestMapping("")
    public String index(){
        return "permission/list";
    }

    /**
     * 具有分页和高级查询
     * @return
     */
    @RequiresPermissions("permission:list")
    @PermissionName("权限数据")
    @RequestMapping("list")
    @ResponseBody
    public PageResult list(QueryObject qo){
        PageResult result = permissionService.query(qo);
        return result;
    }

    /**
     * 加载权限
     * @return 返回JsonResult对象判断是否操作成功
     */
    @RequiresPermissions("permission:load")
    @PermissionName("权限加载")
    @RequestMapping("load")
    @ResponseBody
    public JsonResult load(){
        JsonResult result = new JsonResult();
        try{
            permissionService.load();
        }catch (Exception e){
            e.printStackTrace();
            result.mark("操作失败!");
        }
        return result;
    }

    @RequiresPermissions("permission:queryPermissionByRoleId")
    @PermissionName("根据角色查询权限")
    @RequestMapping("queryPermissionByRoleId")
    @ResponseBody
    public List<Permission> queryPermissionByRoleId(Long roleId){
        return permissionService.queryPermissionByRoleId(roleId);
    }

    @RequiresPermissions("permission:queryPermissionList")
    @PermissionName("查询权限数据")
    @RequestMapping("queryPermissionList")
    @ResponseBody
    public List<Permission> queryPermissionList(){
        return permissionService.selectAll();
    }
}
